// pages/me/updatepwd/updatepwd.js
const app = getApp()
import WxValidate from '../../../utils/WxValidate.js';
import http from "../../../utils/http.js";

const {
  $Message
} = require('../../../component/iview/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    help_status: false,
    oldPwd_focus: false,
    newPwd_focus: false,
    confirmPwd_focus: false,
    oldPwd: '',
    newPwd: '',
    confirmPwd: '',
    angle: 0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  /**
   * 旧密码输入
   */
  oldPwdInput: function(e) {

    this.setData({
      oldPwd: e.detail.value
    });
  },
  /**
   * 新密码输入
   */
  newPwdInput: function(e) {
    this.setData({
      newPwd: e.detail.value
    });
  },
  /**
   * 新密码确认
   */
  confirmInput: function(e) {
    this.setData({
      confirmPwd: e.detail.value
    });
  },
  /**
   * 提交更新密码
   */
  btnUpdate: function(e) {
    let oldPassword = this.data.oldPwd;
    let newPassword = this.data.newPwd;
    let confirmPassword = this.data.confirmPwd;
    if (newPassword == confirmPassword) {
      http.request({
        url: '/user/updatePassword',
        data: {
          'oldPassword': oldPassword,
          'newPassword': newPassword,
          'confirmPassword': confirmPassword,
        },
        success: (result) => {
          wx.showModal({
            content: '密码修改成功请重新登录',
            showCancel:false,
            success: function(res) {
              if (res.confirm) {
                //取消绑定
                http.request({
                  url: '/common/unbindWxId',
                  success: (data) => {
                    wx.removeStorage({
                      key: 'userInfo',
                      complete(res) {
                        console.log(res)
                      }
                    });
                    wx.redirectTo({
                      //redirectTo 关闭修改密码页面  跳转指定页面
                      url: '/pages/me/login/login',
                    });
                  }
                });
              }
            }
          });
        }     
      });


    } else {
      wx.showToast({
        title: '两次密码输入不一致', //提示文字
        duration: 2000, //显示时长
        mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
        icon: 'none', //图标，支持"success"、"loading"  

      })

    }


  },
 

  inputFocus: function(e) {
    let oldpassword = this.data.oldPwd;
    let newpassword = this.data.newPwd;

    if (e.target.id == 'oldPwd') {
      this.setData({
        'oldPwd_focus': true
      });
    } else if (e.target.id == 'newPwd') {
      this.setData({
        'newPwd_focus': true
      });
    } else if (e.target.id == 'confirmPwd') {
      this.setData({
        'confirmPwd_focus': true
      });
    }
  },
  inputBlur: function(e) {

    if (e.target.id == 'oldPwd') {
      this.setData({
        'oldPwd_focus': false
      });
    } else if (e.target.id == 'newPwd') {
      this.setData({
        'newPwd_focus': false
      });
    } else if (e.target.id == 'confirmPwd') {
      let newpwassword = this.data.newPwd;
      let confirmpwd = this.data.confirmPwd;
      if (newpwassword === confirmpwd) {
        this.setData({
          'confirmPwd_focus': false
        });
      } else {
        wx.showToast({
          title: '两次密码输入不一致', //提示文字
          duration: 2000, //显示时长
          mask: true, //是否显示透明蒙层，防止触摸穿透，默认：false  
          icon: 'none', //图标，支持"success"、"loading"  

        })

      }

    }
  },
  tapHelp: function(e) {
    if (e.target.id == 'help') {
      this.hideHelp();
    }
  },
  showHelp: function(e) {
    this.setData({
      'help_status': true
    });
  },
  hideHelp: function(e) {
    this.setData({
      'help_status': false
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // let _this =this;
    // wx.onAccelerometerChange(function (res) {
    //   var angle = -(res.x * 30).toFixed(1);
    //   if (angle > 14) {
    //     angle = 14;
    //   } else if (angle < -14) {
    //     angle = -14;
    //   }
    //   if (_this.data.angle !== angle) {
    //     _this.setData({
    //       angle: angle
    //     });
    //   }
    // });

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})